<?php
namespace frontend\controllers;

use common\components\FrontBaseController;
use common\helps\detection;
use common\helps\qiniu;
use frontend\models\Member;
use frontend\models\District;
use Yii;
use yii\db\Query;
use \common\helps\tools;
use \common\helps\code;
use \common\models\UploadForm;

/**
 * Member controller
 */
class MemberController extends FrontBaseController
{

    public $modelClass = 'frontend\models\Member';
    public function init()
    {
        parent::init();
    }

    /**
     * 获取会员信息
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetMemberInfo(){
        $model = new Member();
        $data = $model->getMemberInfo($this->member_info);
        if(!empty($data)){
            return code::success($data);
        }else{
            return code::error('error','未查询到相关数据');
        }

    }


    /**
     * 获取会员消息列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetNoticeList(){
        $model = new Member();
        $request =$this->request;
        $params = $request->bodyParams;
        $params['member_id'] = $this->member_info['id'];
        $data = $model->getNoticeList($params);
        if(count($data['data'])){
            foreach($data['data'] as &$value){
                $value['posts_img'] = explode(',',$value['posts_img']);
            }
        }
        if(!empty($data['data'])){
            $model->updateMemberNoticeStatus($params['member_id']);
        }
        return code::success($data);
    }


    /**
     * 编辑会员信息
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionEditMemberInfo(){
        $model = new Member();
        $request =$this->request;
        $params = $request->bodyParams;
        $params['member_id'] = intval($this->member_info['id']);
        $params['token'] = $this->member_info['token'];
        if(empty($params['province_code']) || empty($params['city_code'])){
            return code::error('error','请选择省市地址!');
        }else{
            $district_model = new District();
            $district_data = $district_model->getDistrictIdByCode($params['province_code'],$params['city_code']);
            if(!empty($district_data['error'])){
                return code::error('error',$district_data['error']);
            }else{
                $params['province_id'] = intval($params['province_code']);
                $params['city_id'] = intval($params['city_code']);
            }


        }
        $model->scenario = 'update_self_info';
        $model->load($params,'');
        if($model->validate()){
            $data = $model->editMemberInfo($params);
            if(!empty($data['error'])){
                return code::error('error',$data['error']);
            }else{
                return code::success();
            }
        }else{
            $error = array_values($model->getErrors());
            return code::error('',$error[0][0]);
        }



    }


    /**
     * 获取会员评论列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetMemberCommentList(){
        $model = new Member();
        $request =$this->request;
        $params = $request->bodyParams;
        $total_data = $model->getMemberCommentList($this->member_info);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);
        if(count($data['data'])){
            foreach($data['data'] as &$value){
                if(is_null($value['posts_deleted_type'])){
                    $value['posts_deleted_type']=1;
                }else{
                    $value['posts_deleted_type']=2;//帖子已删除
                }
                $value['posts_img'] = explode(',',$value['posts_img']);
            }
        }

        return code::success($data);


    }

    /**
     * 获取会员收藏列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetMemberCollectList(){
        $model = new Member();
        $request =$this->request;
        $params = $request->bodyParams;
        $data = $model->getMemberCollectList($params,$this->member_info);
        return code::success($data);
    }




    /**
     * 会员删除评论
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionDeleteMemberComment(){
        $model = new Member();
        $request =$this->request;
        $params = $request->bodyParams;
        if(empty($params['comment_id']))   return code::error('mistake');
        $data = $model->deleteMemberComment($params,$this->member_info);
        if(!empty($data['error'])){
            return code::error('error',$data['error']);
        }else{
            return code::success();
        }

    }


    /**
     * 用作检测图片
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public function actionCheckImg()
    {
        if(empty($_FILES['file'])){
            return code::error('error','未获取到文件对象');
        }
        $files = $_FILES['file'];
        $detection = new detection();
        $check_res=$detection->checkImg( new \CURLFile($files['tmp_name']));
        if($check_res==false){
            return code::error('error','操作失败');
        }
        if(($check_res['errcode']!=0)){
            return code::error('error','图片含有不合规内容');
        }
        return code::success();

    }
    /**
     * 用户上传头像先本地上传，合规后再上传七牛云
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public function actionAvatarUpload()
    {
        $model = new UploadForm();
        if(empty($_FILES['file'])){
            return code::error('error','未获取到文件对象');
        }
        $files = $_FILES['file'];
        //验证图片是否合规
        $detection = new detection();
        $check_res=$detection->checkImg( new \CURLFile($files['tmp_name']));
        if($check_res==false){
            return code::error('error','操作失败');
        }
        if(($check_res['errcode']!=0)){
            return code::error('error','图片含有不合规内容');
        }

        $model->file =$files;
        $dir=UPLOADS.'member/avatar/';
        $res = $model->upload($dir,$files);
        if($res){
            $qiniu = new qiniu();
            $sExtension = strrchr($files['name'],'.');
            $time=tools::msectime();
            $file_name='usersAvatar/'.$time.$sExtension;
            $data_res=$qiniu->uploadByQiNiu($dir.$res,$file_name);
            @unlink($dir.$res);
        if(empty($data_res['key'])){
            return code::error('error','上传失败');
        }else{
            $data['img_url'] = $data_res['key'];
            return code::success($data);
        }

        }else{
            return code::error('error','图片本地上传失败');
        }

    }


    /*
     * 我的发布
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionMyPosts(){
        $model = new \frontend\models\Posts();
        $member_info = $this->member_info;
        $request =$this->request;
        $params = $request->bodyParams;

        $total = $model::mySelectData(['set_posts_id' => $member_info['id'],'my_posts' => 1],'`posts`.`id`,`posts`.`title`,`posts`.`img`,`posts`.`describe`,`posts`.`status`,`posts`.`set_posts_id`,`posts`.`created_at`,`m`.`username`,`m`.`head_img`');
        $res = tools::getPageData($total,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);
        return code::success($res);
    }

    /*
     * 删除我的发布
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionDelMyPosts(){
        $model = new \frontend\models\Posts();
        $member_info = $this->member_info;
        $request =$this->request;
        $params = $request->bodyParams;

        $res = $model::deleteMyPosts(['id' => $params['id'],'set_posts_id' => $member_info['id']]);
        if(empty($res->errorInfo)){
            return code::success(true);
        }else{
            return code::error('error');
        }
    }

    /*
     * 城市选择
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionChangeCity(){
        $model = new \frontend\models\Weal();
        $member = new \frontend\models\Member();
        $member_info = $this->member_info;
        $request =$this->request;
        $params = $request->bodyParams;

        $res = $member::saveData($member_info['id'],['province_id' => $params['province_code'],'city_id' => $params['city_code']]);
        if(empty($res->errorInfo)){
            return code::success(true);
        }else{
            return code::error('error');
        }
    }



}
